# 会议室预定系统 - 后端快速启动脚本
# Meeting Room Booking System - Backend Quick Start Script

Write-Host "========================================" -ForegroundColor Cyan
Write-Host "会议室预定系统 - 后端启动" -ForegroundColor Cyan
Write-Host "Meeting Room Booking System - Backend" -ForegroundColor Cyan
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""

# 检查是否在正确的目录
$currentDir = Get-Location
if (-not (Test-Path "app\main.py")) {
    Write-Host "❌ 错误: 请在 backend 目录下运行此脚本" -ForegroundColor Red
    Write-Host "Error: Please run this script from the backend directory" -ForegroundColor Red
    exit 1
}

Write-Host "📍 当前目录: $currentDir" -ForegroundColor Green
Write-Host ""

# 步骤1: 检查虚拟环境
Write-Host "步骤 1/5: 检查虚拟环境..." -ForegroundColor Yellow
if (Test-Path ".venv\Scripts\Activate.ps1") {
    Write-Host "✅ 发现虚拟环境，正在激活..." -ForegroundColor Green
    & .\.venv\Scripts\Activate.ps1
} else {
    Write-Host "⚠️  未发现虚拟环境，使用全局Python环境" -ForegroundColor Yellow
    Write-Host "建议创建虚拟环境: python -m venv .venv" -ForegroundColor Yellow
}
Write-Host ""

# 步骤2: 安装依赖
Write-Host "步骤 2/5: 检查并安装依赖..." -ForegroundColor Yellow
Write-Host "正在检查 pydantic-settings..." -ForegroundColor Gray

$pydanticSettings = pip list | Select-String "pydantic-settings"
if (-not $pydanticSettings) {
    Write-Host "⚠️  依赖缺失，正在安装..." -ForegroundColor Yellow
    pip install -r requirements.txt
    Write-Host "✅ 依赖安装完成" -ForegroundColor Green
} else {
    Write-Host "✅ 依赖已安装: $pydanticSettings" -ForegroundColor Green
}
Write-Host ""

# 步骤3: 检查数据库连接
Write-Host "步骤 3/5: 检查数据库..." -ForegroundColor Yellow
Write-Host "⚠️  请确保PostgreSQL数据库已启动" -ForegroundColor Yellow
Write-Host "   - Docker方式: docker-compose up db" -ForegroundColor Gray
Write-Host "   - 本地方式: 确保PostgreSQL服务运行中" -ForegroundColor Gray
$confirm = Read-Host "数据库是否已启动? (y/n)"
if ($confirm -ne "y") {
    Write-Host "请先启动数据库，然后重新运行此脚本" -ForegroundColor Yellow
    exit 0
}
Write-Host ""

# 步骤4: 初始化数据库（可选）
Write-Host "步骤 4/5: 初始化数据库..." -ForegroundColor Yellow
$initDb = Read-Host "是否需要初始化数据库? (y/n)"
if ($initDb -eq "y") {
    Write-Host "正在运行 setup_db.py..." -ForegroundColor Gray
    python setup_db.py
    if ($LASTEXITCODE -eq 0) {
        Write-Host "✅ 数据库初始化成功" -ForegroundColor Green
    } else {
        Write-Host "❌ 数据库初始化失败，请检查错误信息" -ForegroundColor Red
        exit 1
    }
} else {
    Write-Host "⏭️  跳过数据库初始化" -ForegroundColor Gray
}
Write-Host ""

# 步骤5: 启动服务器
Write-Host "步骤 5/5: 启动FastAPI服务器..." -ForegroundColor Yellow
Write-Host "========================================" -ForegroundColor Cyan
Write-Host "🚀 正在启动服务器..." -ForegroundColor Green
Write-Host "========================================" -ForegroundColor Cyan
Write-Host ""
Write-Host "访问地址:" -ForegroundColor Yellow
Write-Host "  - API根路径:   http://localhost:8000" -ForegroundColor Cyan
Write-Host "  - API文档:     http://localhost:8000/docs" -ForegroundColor Cyan
Write-Host "  - 健康检查:    http://localhost:8000/health" -ForegroundColor Cyan
Write-Host ""
Write-Host "按 Ctrl+C 停止服务器" -ForegroundColor Gray
Write-Host ""

# 启动uvicorn
uvicorn app.main:app --reload --host 0.0.0.0 --port 8000
